package components.menus.menubar
{
	import flash.events.MouseEvent;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Box;
	import mx.containers.HBox;
	import mx.containers.Tile;
	import mx.containers.VBox;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.managers.PopUpManager;

	[Event(name="menuSelected", type="components.menus.menubar.MenuBarEvent")]
	
	public class MenuBar extends HBox
	{
		private var controlList:ArrayCollection;
		private var currentMenu:Box;
		private var currentPopUpMenu:Box;
		private var idTimeout:uint;
		
		[Bindable]
        [Embed(source="images/components/menu/menubar/arrow.png")]
		private var iconArrow:Class;
			
		public function MenuBar()
		{
			this.controlList = new ArrayCollection();
			this.styleName = "menuBar";
		}
		
		public function addMenuItem(menuItem:MenuItem):void
		{
			var label:Label = new Label();
			label.text = menuItem.text;
			label.styleName = "menuItem";
						
			var box:Box = new Box();
			box.id = menuItem.id;
			box.setStyle("paddingLeft", this.getStyle("paddingLeft"));
			box.setStyle("paddingRight", this.getStyle("paddingRight"));
			box.buttonMode = true;
			box.useHandCursor = true;
			box.mouseChildren = false;
			box.percentHeight = 90; 
			box.addEventListener(MouseEvent.MOUSE_OVER, mouseOverMenu);
			box.addEventListener(MouseEvent.MOUSE_OUT, mouseOutMenu);
			box.addEventListener(MouseEvent.CLICK, menuSelected); 
			box.addChild(label);

			this.addChild(box);
			
			var object:Object = new Object();
			object.box = box;
			object.menuItem = menuItem;
			object.overMenu = false;
			object.overPopUpMenu = false;
			object.showPopUpMenu = false;
			
			controlList.addItem(object);			
		}
		
		private function getControlObject(box:Box):Object
		{
			var o:Object;
			var found:Boolean;
			
			for(var i:int = 0; i < controlList.length && !found; i = i + 1)
			{
				if(controlList.getItemAt(i).box == box)
				{
					o = controlList.getItemAt(i);
					found = true;
				}
			}
			
			return o;
		}		
		
		private function createPopUpMenu(menuItem:MenuItem):Box
		{
			var popUpMenu:Box = new Box();
			popUpMenu.width = this.width;
			popUpMenu.styleName = "popUpMenu";
			popUpMenu.x = this.x;
			popUpMenu.y = this.y + this.height;	
			popUpMenu.horizontalScrollPolicy = "off";
			popUpMenu.verticalScrollPolicy = "off";
			
			var tile:Tile = new Tile();
			tile.setStyle("borderStyle", "none");
			tile.width = this.width;
			tile.percentHeight = 100;

			var menuSubItem:ArrayCollection = menuItem.subItens;
			
  			for(var i:int = 0; i < menuSubItem.length; i = i + 1)
			{
				var subItem:MenuSubItem = menuSubItem.getItemAt(i) as MenuSubItem;
				
				var label:Label = new Label();
				label.text = subItem.text;
				label.styleName = "popUpMenuItemText";
				
				var image:Image = new Image();
				image.source = iconArrow;
				
				var hBox:HBox = new HBox();
				hBox.setStyle("verticalAlign", "middle");
				hBox.addChild(image);
				hBox.addChild(label);
									
				var boxSubMenu:VBox = new VBox();
				boxSubMenu.id = subItem.id;
				boxSubMenu.addChild(hBox);
				boxSubMenu.addEventListener(MouseEvent.MOUSE_OVER, changeBackgroundOver);
				boxSubMenu.addEventListener(MouseEvent.MOUSE_OUT, changeBackgroundOut);
				boxSubMenu.addEventListener(MouseEvent.CLICK, menuSelected);
				boxSubMenu.styleName = "popUpMenuItem";
				boxSubMenu.useHandCursor = true;
				boxSubMenu.buttonMode = true;
				boxSubMenu.mouseChildren = false;
				
				var description:TextArea = new TextArea();
				description.styleName = "popUpMenuItemDescription";
				description.verticalScrollPolicy = "off";
				description.enabled = false;
				description.text = subItem.description;
				description.width = 200;
				description.percentHeight = 100;
				
				boxSubMenu.addChild(description);				
				
				tile.addChild(boxSubMenu);
			}
			
			popUpMenu.addChild(tile);
			popUpMenu.addEventListener(MouseEvent.MOUSE_OVER, mouseOverPopUpMenu);
			popUpMenu.addEventListener(MouseEvent.MOUSE_OUT, mouseOutPopUpMenu);	
			
			return popUpMenu;			
		}
		
		private function changeBackgroundOver(event:MouseEvent):void
		{
			var boxSubMenu:Box = event.currentTarget as Box;
			boxSubMenu.styleName = "popUpMenuItemOver";
		}
		
		private function changeBackgroundOut(event:MouseEvent):void
		{
			var boxSubMenu:Box = event.currentTarget as Box;
			boxSubMenu.styleName = "popUpMenuItem";
		}			
		
		private function menuSelected(event:MouseEvent):void
		{
			var id:String = Box(event.currentTarget).id;
			var menuBarEvent:MenuBarEvent = new MenuBarEvent(MenuBarEvent.MENU_SELECTED, id);
			dispatchEvent(menuBarEvent);
			
			// Esconde o popup menu
			var o:Object = getControlObject(currentMenu);
			o.overPopUpMenu = false;
			hidePopUpMenu();
		}
		
		private function mouseOverMenu(event:MouseEvent):void
		{
			var currentMenuTemp:Box = event.currentTarget as Box;
			
			if(currentMenuTemp != currentMenu)
			{
				hidePopUpMenu();
			}
			
			currentMenu = currentMenuTemp;
			var o:Object = getControlObject(currentMenu);
			o.overMenu = true;
			
			setTimeout(showPopUpMenu, 600);			
		}
		
		private function mouseOutMenu(event:MouseEvent):void
		{
			var o:Object = getControlObject(currentMenu);
			o.overMenu = false;				
			idTimeout = setTimeout(hidePopUpMenu, 1000);
		}
		
		private function mouseOverPopUpMenu(event:MouseEvent):void
		{
			clearTimeout(idTimeout);
			var o:Object = getControlObject(currentMenu);
			o.overPopUpMenu = true;			
		}
					
		private function mouseOutPopUpMenu(event:MouseEvent):void
		{
			clearTimeout(idTimeout);
			var o:Object = getControlObject(currentMenu);
			o.overPopUpMenu = false;			
			setTimeout(hidePopUpMenu, 2000);			
		}
					
		private function showPopUpMenu():void
		{
			var o:Object = getControlObject(currentMenu);
			
			if(o != null && !o.showPopUpMenu && (o.overMenu || o.overPopUpMenu))
			{	
				currentMenu.setStyle("backgroundColor", "#E2E4EA");

				// Cria pop up menu se existirem menu a ser exibido
				if(o.menuItem.subItens.length > 0)
				{
					o.showPopUpMenu = true;
					currentPopUpMenu = createPopUpMenu(o.menuItem);	
					PopUpManager.addPopUp(currentPopUpMenu, this, false);		
				}	
			}	
		}		
		
		private function hidePopUpMenu():void
		{
			var o:Object = getControlObject(currentMenu);
			
			if(o != null && !o.overMenu && !o.overPopUpMenu)
			{			
				currentMenu.setStyle("backgroundColor", "#FFFFFF");
				o.showPopUpMenu = false;
				PopUpManager.removePopUp(currentPopUpMenu);
			}
		}	
		
	}
}